package com.eightbears.beboy.base;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.eightbears.beboy.R;

import java.io.Serializable;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {


    public void startLocalActivity(Class cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void startLocalActivityAndClear(Class cls) {
        Intent logoutIntent = new Intent(getActivity(), cls);
        logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(logoutIntent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void startLocalActivity( Class cls, String key, Object params) {
        Intent intent = new Intent(getActivity(), cls);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(key, (Serializable) params);
        intent.putExtras(mBundle);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }
    public void startLocalActivity( Class cls, String key, Object params,String key1, Object params1) {
        Intent intent = new Intent(getActivity(), cls);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(key, (Serializable) params);
        mBundle.putSerializable(key1, (Serializable) params1);
        intent.putExtras(mBundle);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void startLocalActivity(Class cls, String[] key, Object[] params) {
        Intent intent = new Intent(getActivity(), cls);
        Bundle mBundle = new Bundle();
        for (int i = 0; i < key.length; i++) {
            mBundle.putSerializable(key[i], (Serializable)params[i]);
        }
        intent.putExtras(mBundle);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }



    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    /**
     * 替换
     *
     * @param id_content
     * @param fragment
     */
    public void replaceFragment(int id_content, Fragment fragment) {
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(id_content, fragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }



}
